<?php defined('BASEPATH') || exit('No direct script access allowed');
class Black extends Admin_Controller
{
	/**
	 * Controller constructor sets the login restriction
	 *
	 */
	
	public function __construct()
	{
		parent::__construct();
		$this->load->model('black_model');
        $this->lang->load('black');
		$this->auth->restrict();
        $this->form_validation->set_error_delimiters("<span class='error'>", "</span>");
        Template::set_block('sub_nav', 'content/_sub_nav'); 
        //Assets::add_module_js('black', 'black.js');
	}
    public function dianhuaBlack(){

    }
	public function index($filter = "all",$offset = 0){  
        if (isset($_POST['delete'])) {
            $checked = $this->input->post('checked');
            if (is_array($checked) && count($checked)) {
                $result = true;
                foreach ($checked as $pid) {
                    $deleted = $this->black_model->delete($pid);
                    if ($deleted == false) {
                        $result = false;
                    }
                }
                if ($result) {
                    Template::set_message(count($checked) . ' ' . lang('black_delete_success'), 'success');
                } else {
                    Template::set_message(lang('black_delete_failure') . $this->black_model->error, 'error');
                }
            }
        }
        $totalRows = $this->black_model->count_all();
        $pagerBaseUrl = site_url('admin/black/index') . '/';
        $limit  = $this->settings_lib->item('site.list_limit') ?: 15;
        $pagerUriSegment = 5;
        $q = $this->input->get('q');
        if (!empty($q)){
            $totalRows = $this->black_model->select()->like('name', $q)->count_all();
            $records = $this->black_model->select()->like('name', $q)->find_all();
        } else {
            $this->black_model;
        }

		$this->load->library('pagination');
        $this->pager['base_url']    = $pagerBaseUrl.$filter."/";
        $this->pager['total_rows']  = $totalRows;
        $this->pager['per_page']    = $limit;
        $this->pager['uri_segment'] = $pagerUriSegment;

        $this->pagination->initialize($this->pager);
        $this->black_model->limit($limit, $offset);
		
        $black = $this->black_model->find_all();
        

        Template::set('black',$black);
        Template::set_view('admin/black/index');
        Template::render();
		
	}
	
	public function  create(){
        Template::set_view('admin/black/create');
        if (isset($_POST['save'])) {
            if ($insert_id = $this->save_black()) {
                log_activity($this->auth->user_id(), lang('black_act_create_record') . ': ' . $insert_id . ' : ' . $this->input->ip_address(), 'black');
                Template::set_message(lang('black_create_success'), 'success');

                redirect('admin/black/index');
            }
            if ( ! empty($this->black_model->error)) {
                Template::set_message(lang('black_create_failure') . $this->black_model->error, 'error');
            }
        }
        Template::set('toolbar_title', lang('black_action_create'));

        Template::render();
    }

    public function delete()
    {
        $id = $this->uri->segment(4);
        
        if (empty($id)) {
            Template::set_message(lang('black_invalid_id'), 'error');

            redirect('admin/black/index');
        }

        $this->auth->restrict($this->permissionDelete);

        if ($this->black_model->delete($id)) {
            
            Template::set_message(lang('black_delete_success'), 'success');

            redirect('admin/black/index');
        }

    }

    public function edit()
    {
        $id = $this->uri->segment(4);
        if (empty($id)) {
            Template::set_message(lang('black_invalid_id'), 'error');

            redirect('admin/black/index');
        }
        
        if (isset($_POST['save'])) {
            $this->auth->restrict($this->permissionEdit);

            if ($this->save_black('update', $id)) {
                log_activity($this->auth->user_id(), lang('black_act_edit_record') . ': ' . $id . ' : ' . $this->input->ip_address(), 'black');
                Template::set_message(lang('black_edit_success'), 'success');
                redirect('admin/black/index');
            }

            // Not validation error
            if ( ! empty($this->black_model->error)) {
                Template::set_message(lang('black_edit_failure') . $this->black_model->error, 'error');
            }
        }
        
        
        $data = $this->black_model->find($id);

        Template::set('data',$data);
        Template::set_view('admin/black/edit');
        Template::render();
    }
    

    private function save_black($type = 'insert', $id = 0)
    {
        if ($type == 'update') {
            $_POST['id'] = $id;
        }

        $this->form_validation->set_rules($this->black_model->get_validation_rules());
        if ($this->form_validation->run() === false) {
            return false;
        }

        // Make sure we only pass in the fields we want
        
        $data = $this->black_model->prep_data($this->input->post());

        // Additional handling for default values should be added below,
        // or in the model's prep_data() method
        

        $return = false;
        if ($type == 'insert') {
            $id = $this->black_model->insert($data);

            if (is_numeric($id)) {
                $return = $id;
            }
        } elseif ($type == 'update') {
            $return = $this->black_model->update($id, $data);
        }

        return $return;
    }



}
